package com.login;

import com.test.R;
import com.test.StageTabListAvtivity;
import com.utils.Downloader;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ConfigActivity extends Activity {

	private Downloader downloader;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.configure);
		Button configBtn = (Button) findViewById(R.id.confBtn);

		configBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				EditText serverEditText = (EditText) findViewById(R.id.serverIP);
				EditText userEditText = (EditText) findViewById(R.id.userId);
				String server = serverEditText.getText().toString();
				String user = userEditText.getText().toString();

				// this just catches the error if the program cant locate the
				// GUI stuff
				if (user.equals("") || server.equals("")) {
					Toast message = Toast.makeText(getApplicationContext(),
							R.string.login_uncompleted, Toast.LENGTH_SHORT);
					message.setGravity(Gravity.CENTER,
							message.getXOffset() / 2, message.getYOffset() / 2);
					message.show();
				} else
				{
					downloader=new Downloader();
					int result=downloader.config(server, user,"config/","ece_configuration.conf");
					if(result==0){
						//save the configuration
						Intent intent = new Intent(getApplicationContext(),StageTabListAvtivity.class);
						startActivity(intent);
					}else{
						Toast.makeText(getApplicationContext(), "Failed to Create Configure File",Toast.LENGTH_SHORT).show();
					}
					
				}

			}

		}

		);
	}
}
